﻿//Libraries that are being used
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Snippets_Generator_for_Denali
{
    public partial class frmMain : Form
    {
        //Initialize XML Expression
        String xmlExpression = "";

        //Initializations
        public frmMain()
        {
            InitializeComponent();
            cmbSnippetType.SelectedIndex = 0;
            
            //Initialize user input components
            txtAuthor.Text = "";
            txtDescription.Text = "";
            txtTitle.Text = "";
            txtSnippetCode.Text = "";
        }

        /// <summary>
        /// Handles the click on the "Generate Snippet" button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenSnippet_Click(object sender, EventArgs e)
        {

            //Check if a snippet is ready to be generated
            if (txtSnippetCode.Text == "")
            {
                MessageBox.Show(this, "You cannot export an empty snippet!", "Snippets Generator - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            else
            {

                //************************************************
                //Dynamic Construction of Snippet XML Expression *
                //************************************************            
                //Header
                xmlExpression = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <CodeSnippets xmlns=\"http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet\"> <CodeSnippet Format=\"1.0.0\"> <Header>";

                //Title
                xmlExpression += "<Title>" + txtTitle + "</Title>";

                //Shortcut
                xmlExpression += "<Shortcut></Shortcut>";

                //Description
                xmlExpression += "<Description>" + txtDescription + "</Description>";

                //Author
                xmlExpression += "<Author>" + txtAuthor + "</Author> ";

                //Snippet Types 1
                xmlExpression += "<SnippetTypes>";

                //Snippet Types 2
                xmlExpression += "<SnippetType>" + cmbSnippetType.SelectedItem.ToString() + "</SnippetType>";

                //Snippet Types 3
                xmlExpression += "</SnippetTypes></Header><Snippet>";

                //T-SQL Code - Header
                xmlExpression += "<Code Language=\"SQL\"><![CDATA[";

                //T-SQL Code - Snippet Code
                xmlExpression += txtSnippetCode;

                //End XML Expression
                xmlExpression += "]]></Code></Snippet></CodeSnippet></CodeSnippets>";
                //************************************************


                //Generate the snippet file            
                dlgSaveFile.Filter = "snippet files (*.snippet)|*.snippet";
                dlgSaveFile.ShowDialog(this);


                try
                {
                    String fileName = dlgSaveFile.FileName.ToString();
                    StreamWriter fileHandler = new StreamWriter(fileName.ToString());

                    fileHandler.WriteLine(xmlExpression);
                    fileHandler.Close();

                    MessageBox.Show("Snippet was successfully generated!", "Snippet Generator v0.1", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    //Reinitialize form
                    txtAuthor.Text = "";
                    txtDescription.Text = "";
                    txtSnippetCode.Text = "";
                    txtTitle.Text = "";
                    dlgSaveFile.FileName = "";

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Snippets Generator v1.0 - Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw;
                }
            }

        }

        /// <summary>
        /// Exits the application.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Exits the application.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Creates a new snippet instance.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnItemNewSnippet_Click(object sender, EventArgs e)
        {
            //Reinitialize form
            txtAuthor.Text = "";
            txtDescription.Text = "";
            txtSnippetCode.Text = "";
            txtTitle.Text = "";
            dlgSaveFile.FileName = "";
        }

       /// <summary>
       /// Information about the program.
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void mnItemAbout_Click(object sender, EventArgs e)
        {
            //About this program
            String aboutTxt = "This utility allows generating snippets that can be used in SQL Server \"Denali\" RC0 or later.";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "Disclaimer: This software is under the Microsoft Public License (Ms-PL). The author of this program will not be responsible for illegal actions or damage caused by the misuse of this software.";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "Author's Email Address: a_artemiou@hotmail.com";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "Project's website: http://snippetsgen.codeplex.com";

            MessageBox.Show(aboutTxt, "Snippets Generator v1.0 - About the Program", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Information on how to import a snippet.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnItemImportingSnippet_Click(object sender, EventArgs e)
        {
            //About this program
            String aboutTxt = "For being able to make use of the generated snippets, the only prerequisite is that you are using SQL Server 2012 Management Studio (SSMS).";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "Procedure for importing the generated snippet in SSMS";
            aboutTxt += Environment.NewLine;
            aboutTxt += "----------------------------------------------------------";
            aboutTxt += Environment.NewLine;
            aboutTxt += "1: Create a new folder and save the generated snippet file(s) there.";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "2: In SSMS, click on \"Tools\" and then select \"Code Snippet Manager\".";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "3: Click on the \"Add\" button and select to add the folder you earlier created and which contains the generated snippet file(s).";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "4: Click on the \"OK\" button and select to add the folder you earlier created and which contains the generated snippet file(s).";
            aboutTxt += Environment.NewLine;
            aboutTxt += Environment.NewLine;
            aboutTxt += "5: That's it! You can now start using your own T-SQL code snippets!";

            MessageBox.Show(aboutTxt, "Snippets Generator v1.0 - Importing a Snippet", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
